import { getToken } from '@/utils/auth';
import request from '@/http/request';
const user = {
    state: {
        userinfo: {},
        token: getToken(),
        nickname: null,
        phoneNumber: '',
    },
    mutations: {
        SET_USER: (state, user) => {
            state.nickname = user.username;
            state.userinfo = user;
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
            localStorage.setItem('token', token);
        },
        SET_PHONE: (state, phone) => {
            state.phoneNumber = phone;
        },
    },
    actions: {
        GetUserInfo({ commit }) {
            return new Promise(resolve => {
                request.post('/api/user/userinfo').then(res => {
                    const { data } = res.data;
                    commit('SET_USER', data.user);
                    resolve();
                });
            });
        },
    },
};

export default user;
